﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MultiConvert
{
    internal class MSWDocumentConverter : MultiConvertDocumentConverter
    {
        private IMswDocument Document;
        private MSWordWrapper Application;
        private String ToFile;

        internal MSWDocumentConverter(IMswDocument document, MSWordWrapper parent)
        {
            this.Document = document;
            this.Application = parent;
        }
        
        internal override String convert(String convertMethod, String toFileName)
        {
            this.ToFile = toFileName;
            try
            {
                switch (convertMethod)
                {
                    case "doc2pdf":
                        this.Document.SaveAs(toFileName, WDSaveFormat.wdFormatPDF);
                        break;
                    case "doc2doc":
                        this.Document.SaveAs(toFileName, WDSaveFormat.wdFormatDocument);
                        break;
                    default:
                        this.ConversionResult = Properties.Resources.MsgNoConvImplemented;
                        break;
                }               
                this.ConversionResult = Properties.Resources.MsgConvSuccess;
            }
            catch (System.IO.IOException ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.ToString());
                this.ConversionResult = Properties.Resources.MsgConvFailed;
            }
            return this.ConversionResult;
        }        

        public String Path
        {
            get
            {
                return this.Document.Path;
            }
        }

        public Boolean RefreshFields()
        {
            IMswTemplate DocumentTemplate = (IMswTemplate)this.Document.AttachedTemplate;
            
            String DocumentTemplateName = DocumentTemplate.Name;
			String DocumentName = this.Document.Name;
#if TRACE   
			IMswTemplate NormalTemplate = this.Application.Application.NormalTemplate;
            String NormalTemplateName = NormalTemplate.Name;
#endif      
            // Normal.dotm zuweisen, um ggf. Netzwerkpfad wg. langer Öffnungszeit zu eleminieren.
            // Abfrage notwendig, da sonst Exception geworfen wird
            if ( DocumentTemplateName != DocumentName )
            {
            	this.Document.AttachedTemplate = this.Application.Application.NormalTemplate;
            }
            
            IMswSections Sections = this.Document.Sections;
            IMswSection Section;
            IMswHeadersFooters Headers;
            IMswHeaderFooter Header;
            IMswRange Range;
            IMswFields Fields;

            if (Sections.Count > 0)
            {
                Section = Sections.Item(1);
            }
            else return false;

            Headers = Section.Headers;
            for(int i = 1; i <= Headers.Count; i++)
            {
                Header = Headers.Item(i);
                Range = Header.Range;
                Fields = Range.Fields;
                Fields.Update();
            }            
            return true;
        }
                
    }
    
}
